<script type="text/javascript">
/* Page init setting for User */
var initUrl = "<?= Zend_Registry::get('baseUrl')?>/admin/user/query";
var initParam = "currentpage=1";
var columnName = new Array("userid", "username", "email", "status", "rolename", "projname");
var operate = "edit,delete";
var currentPage = "1";

$(document).ready(function() {
	fillData(initUrl, initParam, columnName, operate, currentPage);
	markDisabled(Array("edit"));
	$("#custname").hide();

	$("#projname").toggle(
		function() {
			$("#allproject").show();
		},
		function() {
			$("#allproject").hide();
	});
});

/* Fill data to action panel for Edit */
function goEdit(obj, isEdit) {
	var dataRow = $(obj).parent().parent().children();
	var param = "userid=" + dataRow.eq(0).text();
	$.post(initUrl, param, function (data) {
		var json = eval(data);
    	var lenJson = json.length - 1;
    	if (lenJson == 1) {
			$("#userid").val(json[0]['userid']);
			$("#username").val(json[0]['username']);
			$("#email").val(json[0]['email']);
			$("#status").val(json[0]['status']);
			$("#roleid").val(json[0]['roleid']);
			var projectArray = json[0]['projname'].split("<br>");
			$("input[type=checkbox]").attr("checked", false);
			var projectname = "";
			for (var key in projectArray) {
				$("input[type=checkbox][name=projectlist][value=" + projectArray[key] + "]").attr("checked", true);
				projectname += projectArray[key] + ",";
			}
			var lastIndex = projectname.lastIndexOf(",,");
			$("#projname").val(projectname.substr(0, lastIndex));
			$("#custname").val(json[0]['custname']);
			$("input[type=radio][name=customerlist][value=" + json[0]['custname'] + "]").attr("checked", true);
    	}
    	else {
    		$("#msg").text("<?= $this->translate('message.common.nodata')?>");
    	}
	});
	
	if (isEdit) {
		markEnabled(Array("edit"));
		markDisabled(Array("add", "userid", "username"));
	}
	else {
		markDisabled(Array("edit"));
		markEnabled(Array("add", "userid", "username"));
	}
	$("#password").val("");
	$("#actionPanel").show();
	$("#allproject").hide();
	$("#msg").text("<?= $this->translate('tip.common.action')?>");
	location.href = "#top";
}

/* Add new data */
function doAdd() {
    if (isNull("username")) {
    	$("#msg").text("<?= $this->translate('message.user.username.null')?>");
    	return false;
    }
    if (isNull("password")) {
        $("#msg").text("<?= $this->translate('message.user.password.null')?>");
    	return false;
    }
    if (isNull("email")) {
        $("#msg").text("<?= $this->translate('message.user.email.null')?>");
    	return false;
    }
    if (isNull("status")) {
        $("#msg").text("<?= $this->translate('message.user.status.null')?>");
    	return false;
    }
    if (isNull("roleid")) {
        $("#msg").text("<?= $this->translate('message.user.roleid.null')?>");
    	return false;
    }
    if (isNull("projname")) {
        $("#msg").text("<?= $this->translate('message.user.project.null')?>");
        $("#allproject").show();
    	return false;
    }
    var userproject = "";
    $("input[type=checkbox][name=projectlist][checked]").each(function(){    
    	userproject += $(this).attr("id") + ",";    
	});
	var lastIndex = userproject.lastIndexOf(",");
	var userproject = userproject.substr(0, lastIndex);
	var customerid = $("input[type=radio][name=customerlist][checked]").attr("id");
	var url = "<?= Zend_Registry::get('baseUrl')?>/admin/user/add";
	var param = "username=" + $.trim($("#username").val()) + 
			"&password=" + $.trim($("#password").val()) + 
			"&email=" + $.trim($("#email").val()) +
			"&status=" + $("#status").val() +
			"&roleid=" + $.trim($("#roleid").val()) +
			"&userproject=" + userproject +
			"&customerid=" + customerid;
    $.post(url, param, function (data) {
    	switch (data) {
    	case "user_exist":
    		$("#msg").text("<?= $this->translate('message.user.username.exist')?>");
    		$("#username").focus();
    		break;
    	case "email_exist":
    		$("#msg").text("<?= $this->translate('message.user.email.exist')?>");
    		$("#email").focus();
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.add.failed')?>" + "Null");
    		break;
    	default:
    		fillData(initUrl, initParam, columnName, operate, currentPage);
    		$("#userid").val(data);
    		$("#msg").text("<?= $this->translate('message.common.add.successful')?>" + data);
    	}
    });
}

/* Edit existing data */
function doEdit() {
	if (isNull("username")) {
	    $("#msg").text("<?= $this->translate('message.user.username.null')?>");
		return false;
    }
	if (isNull("email")) {
	    $("#msg").text("<?= $this->translate('message.user.email.null')?>");
		return false;
    }
    if (!isNull("password")) {
    	if(!confirm("<?= $this->translate('message.user.password.confirm')?>"))
    		return false;
    }
    if (isNull("status")) {
        $("#msg").text("<?= $this->translate('message.user.status.null')?>");
    	return false;
    }
    if (isNull("roleid")) {
        $("#msg").text("<?= $this->translate('message.user.roleid.null')?>");
    	return false;
    }
    if (isNull("projname")) {
        $("#msg").text("<?= $this->translate('message.user.project.null')?>");
        $("#allproject").show();
    	return false;
    }
    var userproject = "";
    $("input[type=checkbox][name=projectlist][checked]").each(function(){    
    	userproject += $(this).attr("id") + ",";    
	});
    var userid = $.trim($("#userid").val());
	var lastIndex = userproject.lastIndexOf(",");
	var userproject = userproject.substr(0, lastIndex);
	var customerid = $("input[type=radio][name=customerlist][checked]").attr("id");
	var url = "<?= Zend_Registry::get('baseUrl')?>/admin/user/edit";
	var param = "userid=" + userid + 
			"&password=" + $.trim($("#password").val()) + 
			"&email=" + $.trim($("#email").val()) +
			"&status=" + $("#status").val() +
			"&roleid=" + $.trim($("#roleid").val()) +
			"&userproject=" + userproject +
			"&customerid=" + customerid;
    $.post(url, param, function (data) {
	    if (data == 1) {
	    	fillEdit(initUrl, "userid=" + userid, columnName, userid);
	    	$("#msg").text("<?= $this->translate('message.common.edit.successful')?>" + $("#userid").val());
	    }
	    else
	    	$("#msg").text("<?= $this->translate('message.common.edit.failed')?>" + $("#userid").val());
	});
}

/* Delete data according to ID */
function doDelete(obj) {
	if(!confirm("<?= $this->translate('message.common.delete.confirm')?>"))
		return false;
	var dataRow = $(obj).parent().parent().children();
	var userid = $.trim(dataRow.eq(0).text());
	url = "<?= Zend_Registry::get('baseUrl')?>/admin/user/delete";
	param = "userid=" + userid;
	$.post(url, param, function(data) {
		switch (data) {
		case "1":
			$("#msg").text("<?= $this->translate('message.common.delete.successful')?>" + userid);
			dataRow.remove();
			$("#totalRecord").text($("#totalRecord").text() - 1);
			break;
		case "error":
			$("#msg").text("<?= $this->translate('message.common.delete.failed')?>" + userid);
			break;
		case "cant_delete_admin":
			$("#msg").text("<?= $this->translate('message.user.delete.admin')?>");
			break;
		default:
    		$("#msg").text(data);
    	}
	});
}

/* Search data */
function doSearch(isSearch, currentPage) {
	if (isSearch) {
		$("#cacheDiv").removeData("cacheData");
		var cacheArray = new Array();
		cacheArray['userid'] = $.trim($("#userid").val());
		cacheArray['username'] = $.trim($("#username").val());
		cacheArray['email'] = $.trim($("#email").val());
		cacheArray['status'] = $.trim($("#status").val());
		cacheArray['roleid'] = $.trim($("#roleid").val());
		cacheArray['projectlist'] = $.trim($("#projectlist").val());
		$("#cacheDiv").data("cacheData", cacheArray);
	}
	var cacheData = $("#cacheDiv").data("cacheData");
	if (typeof(cacheData) == "undefined")
		var param = "currentpage=" + currentPage;
	else
		var param = "currentpage=" + currentPage +
				"&userid=" + cacheData['userid'] + 
		    	"&username=" + cacheData['username'] + 
		    	"&email=" + cacheData['email'] + 
		    	"&status=" + cacheData['status'] +
		    	"&roleid=" + cacheData['roleid'] +
		    	"&projectlist=" + cacheData['projectlist'];
	fillData(initUrl, param, columnName, operate, currentPage);
	$("#msg").text("<?= $this->translate('tip.common.action')?>");
}

/* Select project for the user */
function doSelect(type) {
	if (type == "project") {
    	var projectname = "";
    	$("input[type=checkbox][name=projectlist][checked]").each(function(){    
    		projectname += $(this).attr("value") + ",";    
    	});
    	var lastIndex = projectname.lastIndexOf(",");
    	$("#projname").val(projectname.substr(0, lastIndex));
    	$("#allproject").hide();
	}
	if (type == "customer") {
		selectedCustomer = $("input[type=radio][name=customerlist][checked]").val();
		$("#custname").val(selectedCustomer);
		$("#allcustomer").hide();
	}
}

/* Show the Customer list */
function showCustomer() {
	if ($("#roleid>option:selected").text() == "Customer") {
		$("#allcustomer").show();
		$("#custname").show();
	}
	else {
		$("#allcustomer").hide();
		$("#custname").hide();
		$("input[type=radio][name=customerlist]").attr("checked", false);
	}
}

</script>
<table class="table-action">
<thead>
<tr>
	<a name="top"></a>
    <td colspan="5">
        <?= $this->translate('common.page.panel')?>:&nbsp;&nbsp;
    	<label id="hideShow" />
    </td>
</tr>
</thead>
<tbody>
<tr>
	<td width="14%"><?= $this->translate('column.common.id')?>:</td>
	<td width="36%"><input type="text" id="userid" class="short" title="<?= $this->translate('tip.common.forsearch')?>" /></td>
	<td width="15%"><?= $this->translate('column.user.username')?>:</td>
	<td width="25%" align="right"><input type="text" id="username" maxlength="30" class="short" /></td>
	<td width="10%"></td>
</tr>
<tr>
	<td><?= $this->translate('column.user.password')?>:</td>
	<td><input type="text" id="password" maxlength="20" class="short" title="<?= $this->translate('tip.user.password.reset')?>" /></td>
	<td><?= $this->translate('column.user.email')?>:</td>
	<td align="right"><input type="text" id="email" maxlength="40" class="short" /></td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.user.role')?>:</td>
	<td>
	<select id="roleid" onchange="showCustomer()" class="short">
		<option value=""></option>
		<?php 
		    foreach ($this->role as $role) {
                echo '<option value="' , $role['roleid'] , '">' , $role['rolename'] , '</option>';
		    }
        ?>
		</select><br />
		<input type="text" id="custname" value="" class="short" onclick="$('#allcustomer').show()" readonly />
		<div id="allcustomer" class="itemlist">
		<?php
		    foreach ($this->customer as $customer) {
			    echo '<input type="radio" class="radio" name="customerlist" id="' , $customer['customerid'] , '" value="' , $customer['custname'] , '" />' , $customer['custname'] , '<br />';
		    }
		?><hr />
		<input type="button" onclick="doSelect('customer')" value="<?= $this->translate('common.button.ok')?>" />&nbsp;&nbsp;
		<input type="button" onclick="$('#allcustomer').hide()" value="<?= $this->translate('common.button.cancel')?>" />
		</div>
	</td>
	<td><?= $this->translate('column.user.status')?>:</td>
	<td align="right">
	<select id="status" class="short" title="<?= $this->translate('tip.user.status.locked')?>">
		<option value=""></option>
		<option value="1" selected>Normal</option>
		<option value="0">Locked</option>
	</select></td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.user.project')?>:</td>
	<td colspan="3">
		<input type="text" id="projname" class="long" readonly />
		<div id="allproject" class="itemlist">
		<?php
		    foreach ($this->project as $project) {
			    echo '<input type="checkbox" class="checkbox" name="projectlist" id="' , $project['projectid'] , '" value="' , $project['projname'] , '" />&nbsp;&nbsp;' , $project['projname'] , '<br />';
		    }
		?><hr />
		<input type="button" onclick="doSelect('project')" value="<?= $this->translate('common.button.ok')?>" />&nbsp;&nbsp;
		<input type="button" onclick="$('#allproject').hide()" value="<?= $this->translate('common.button.cancel')?>" />
		</div>
	</td>
	<td></td>
</tr>
</tbody>
<tfoot>
<tr>
	<td><?= $this->translate('common.page.operate')?>:</td>
	<td><span id="actionButton" /></td>
	<td colspan="3">
		<span id="msg" class="message"><?= $this->translate('tip.common.action')?></span>
		<span id="loading" class="loading"><img src="<?= Zend_Registry::get('baseDir')?>/Common/Image/loading.gif" /></span>
	</td>
</tr>
</tfoot>
</table>

<br />
<table class="table-data" id="dataTable">
<thead>
<tr>
    <th class="id"><?= $this->translate('column.common.id')?></th>
    <th><?= $this->translate('column.user.username')?></th>
    <th><?= $this->translate('column.user.email')?></th>
    <th><?= $this->translate('column.user.status')?></th>
    <th><?= $this->translate('column.user.role')?></th>
    <th><?= $this->translate('column.user.project')?></th>
    <th><?= $this->translate('column.common.operate')?></th>
</tr>
</thead>
<tbody id="dataPanel"></tbody>
<tfoot id="footer"></tfoot>
</table>